#pragma once
#include <graphics.h>
#include<vector>
#include"Vector2.h"

class Atlas
{
public:
    Atlas(LPCTSTR path, int num)
    {
        TCHAR path_file[256];
        for (size_t i = 0; i < num; i++)
        {
            _stprintf_s(path_file, path, i);

            IMAGE* frame = new IMAGE();
            loadimage(frame, path_file);
            frame_list.push_back(frame);
        }
    }

    ~Atlas()
    {
        for (size_t i = 0; i < frame_list.size(); i++)
            delete frame_list[i];
    }
public:
    std::vector<IMAGE*> frame_list;
};


Atlas* atlas_Koishi;    Atlas* atlas_left_Koishi;   Atlas* atlas_right_Koishi;
Atlas* atlas_bullet_main;   Atlas* atlas_bullet_sub;

Atlas* atlas_BlueFairy; Atlas* atlas_BlueFairy_left;    Atlas* atlas_BlueFairy_right;
Atlas* atlas_RedFairy;  Atlas* atlas_RedFairy_left;     Atlas* atlas_RedFairy_right;
Atlas* atlas_GreatFairy; Atlas* atlas_GreatFairy_left; Atlas* atlas_GreatFairy_right;
Atlas* atlas_Boss; Atlas* atlas_Boss_left; Atlas* atlas_Boss_right;

Atlas* atlas_danmu_red_small;   Atlas* atlas_danmu_blue_small;  Atlas* atlas_danmu_green_small;
Atlas* atlas_Troll;
Atlas* atlas_item_power;    Atlas* atlas_item_point;    Atlas* atlas_item_hp;
Atlas* atlas_SP_POWER;      Atlas* atlas_SP_HEALTH;

//渲染透明度的动画绘制函数
inline void putimage_alpha(const Vector2& pos, IMAGE* img)
{
    int w = img->getwidth();
    int h = img->getheight();
    AlphaBlend(GetImageHDC(NULL), (int)pos.x, (int)pos.y, w, h,
        GetImageHDC(img), 0, 0, w, h, { AC_SRC_OVER,0,255,AC_SRC_ALPHA });
}

//动画类对象
class Animation
{
public:
    Animation(Atlas* atlas, int interval)
    {
        anim_atlas = atlas;
        interval_ms = interval;
    }

    ~Animation() = default;

    void Play(const Vector2& pos, int delta)
    {
        timer += delta;
        if (timer >= interval_ms)
        {
            idx_frame = (idx_frame + 1) % anim_atlas->frame_list.size();
            timer = 0;
        }
        putimage_alpha(pos, anim_atlas->frame_list[idx_frame]);
    }

private:
    int timer = 0;//动画计时器
    int idx_frame = 0;//动画化帧索引
    int interval_ms = 0;

private:
    Atlas* anim_atlas;
};
